package com.zivwu.library.ui.photo;

import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zivwu.library.R;
import com.zivwu.library.dao.bean.Photo;

import java.util.List;


public class PhotoAdapter extends BaseQuickAdapter<Photo, BaseViewHolder> {
    private static final String TYPE_GIF = "image/gif";
    RequestOptions requestOptions;


    public PhotoAdapter(@Nullable List<Photo> data) {
        super(R.layout.ziv_item_photo, data);
        requestOptions = new RequestOptions();
        requestOptions
                .centerCrop();
    }

    @Override
    protected void convert(BaseViewHolder helper, Photo item) {
        ImageView imageView = (ImageView) helper.getView(R.id.iv_photo);
        helper.addOnClickListener(R.id.iv_checkbox);
        helper.addOnClickListener(R.id.iv_photo);

        if (item.isChecked()) {
            helper.setImageResource(R.id.iv_checkbox, R.drawable.ic_checked);
        } else {
            helper.setImageResource(R.id.iv_checkbox, R.drawable.ic_check);
        }


        RequestManager manager = Glide.with(mContext);
        RequestBuilder builder;
        if (item.getMime_type().equals(TYPE_GIF)) {
            builder = manager.asGif();
        } else {
            builder = manager.asBitmap();
        }
        builder.load(item.getData())
                .apply(requestOptions)
                .thumbnail(0.1f)
                .into(imageView);

    }

}
